/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.controller.StaffCtlr;
import org.gov.vis.master.controller.StaffCtlrImpl;
import org.gov.vis.master.dto.StaffDto;
import org.gov.vis.master.gui.EditStaffDialog;
import org.gov.vis.master.gui.ViewStaffDialog;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewStaffListener implements ActionListener,BaseListener {

	
	private StaffCtlr staffCtlr = new StaffCtlrImpl();
	private ViewStaffDialog viewStaffDialog;
	private StaffDto staffDto;

	final private ResourceBundle rb = Util.getResources();
	private EditStaffDialog editStaffDialog;

	public ViewStaffListener(ViewStaffDialog viewStaffDialog) {
		// TODO Auto-generated constructor stub
		this.viewStaffDialog = viewStaffDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewStaffDialog _viewStaffDialog =(ViewStaffDialog)_guiObject;
		staffDto = new StaffDto();
		
		String code = _viewStaffDialog.getCodeTxtBx().getText();
		if(code!=null){
		 staffDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		StaffDto _staffDto = (StaffDto)_dtoObject;
		if(editStaffDialog != null){
			editStaffDialog.getTxtFldCode().setText(_staffDto.getCode());
			editStaffDialog.getDtChoDateOfJoin().setDate(_staffDto.getDoj());
			editStaffDialog.getTxtFldDistrict().setText(_staffDto.getDistrict());
			editStaffDialog.getTxtFldDesignaton().setText(_staffDto.getDesignation());
			editStaffDialog.getDtChoDateOfBirth().setDate(_staffDto.getDob());
			editStaffDialog.getTxtFldMandal().setText(_staffDto.getMandal());
			editStaffDialog.getTxtFldFatherName().setText(_staffDto.getFatherName());
			editStaffDialog.getTxtFldName().setText(_staffDto.getName());
			editStaffDialog.getTxtFldQualification().setText(_staffDto.getQualificatiion());
			editStaffDialog.getTxtFldVillage().setText(_staffDto.getVillage());
			editStaffDialog.getCombxEmployeeType().setSelectedIndex(Integer.parseInt(_staffDto.getEmploymentType()));
			editStaffDialog.setHidnObject(_staffDto.getStaffId());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewStaffDialog);
			try {
				
			List<StaffDto> staffLst = staffCtlr.searchStaffs(staffDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Name");
			colLst.add("Designation");
			colLst.add("Date of Join");
			colLst.add("District");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(staffLst != null && !staffLst.isEmpty()){
				for(StaffDto dto:staffLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getName());
					lst.add(dto.getDesignation());
					lst.add(dto.getDoj());
					lst.add(dto.getDistrict());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewStaffDialog.getPnlStaffResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			StaffDto staffDto = (StaffDto)visHyperLink.getObject();
		//	System.out.println("action from the object----"+staffDto.getCode());
			editStaffDialog = new EditStaffDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_staff_title"),
                    false);
			setDtoDetailsToGui(staffDto);
			viewStaffDialog.dispose();
		}
		
	}



}
